package org.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.entity.BackgroundCheck;
import org.example.demo.mapper.BackgroundCheckMapper;
import org.example.demo.service.BackgroundCheckService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 政审信息服务实现类
 */
@Service
public class BackgroundCheckServiceImpl extends ServiceImpl<BackgroundCheckMapper, BackgroundCheck>
        implements BackgroundCheckService {

    @Override
    public IPage<BackgroundCheck> selectPage(Integer pageNum, Integer pageSize, String name, String type, String result) {
        // 创建分页对象
        Page<BackgroundCheck> page = new Page<>(pageNum, pageSize);
        
        // 构建查询条件
        LambdaQueryWrapper<BackgroundCheck> queryWrapper = new LambdaQueryWrapper<>();
        
        // 根据姓名模糊查询
        if (StringUtils.hasText(name)) {
            queryWrapper.like(BackgroundCheck::getName, name);
        }
        
        // 根据政审类型精确查询
        if (StringUtils.hasText(type)) {
            queryWrapper.eq(BackgroundCheck::getType, type);
        }
        
        // 根据政审结果精确查询
        if (StringUtils.hasText(result)) {
            queryWrapper.eq(BackgroundCheck::getResult, result);
        }
        
        // 按创建时间降序排序
        queryWrapper.orderByDesc(BackgroundCheck::getCreateTime);
        
        // 执行分页查询
        return page(page, queryWrapper);
    }

    @Override
    public int selectCount(String name, String type, String result) {
        return baseMapper.selectCount(name, type, result);
    }

    @Override
    public BackgroundCheck getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public BackgroundCheck getByApplicantId(Long applicantId) {
        return baseMapper.selectByApplicantId(applicantId);
    }

    @Override
    public boolean saveOrUpdate(BackgroundCheck backgroundCheck) {
        if (backgroundCheck.getId() == null) {
            // 新增记录，设置创建时间
            backgroundCheck.setCreateTime(LocalDateTime.now());
        }
        // 无论新增还是更新，都设置更新时间
        backgroundCheck.setUpdateTime(LocalDateTime.now());
        
        return super.saveOrUpdate(backgroundCheck);
    }

    @Override
    public boolean removeById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public Map<String, Integer> countByResults() {
        // 查询所有政审记录
        List<BackgroundCheck> list = list();
        
        // 按政审结果分组并统计
        Map<String, Long> resultCounts = list.stream()
                .collect(Collectors.groupingBy(
                        BackgroundCheck::getResult,
                        Collectors.counting()
                ));
        
        // 转换结果类型为Map<String, Integer>
        Map<String, Integer> resultMap = new HashMap<>();
        resultCounts.forEach((key, value) -> resultMap.put(key, value.intValue()));
        
        return resultMap;
    }
} 